<template>
    <view class="personInfo full-height">
        <u-image
          class="fixed-img"
          mode="scaleToFill"
          width="100%"
          height="100%"
          :src="`${host}/static/img${douyin}/5.png`"
        ></u-image>

        <view class="fixed-content content">
            <u-form :model="form" ref="uForm" label-align="right" label-width="auto"
                :label-style="{ color: '#fff', fontSize: '40rpx', fontFamily: '黑体' }">
                <u-form-item label="昵称：" prop="nickname">
                    <u-input v-model="form.nickname" :border="true" :placeholder="null" class="custom-input" />
                </u-form-item>
                <u-form-item label="年龄：" prop="submitAge">
                    <u-input v-model="form.submitAge" :border="true" :placeholder="null" type="number"
                        class="custom-input" />
                </u-form-item>
            </u-form>
        </view>
        <u-button class="report-btn" type="warning" @click="jumpResult">生成报告</u-button>
        <custom-loading v-if="loading" />
    </view>
</template>

<script>
    import {mapState, mapGetters, mapMutations} from "vuex";
    import customLoading from "./components/loading.vue";
	var app = getApp();
    export default {
        components: {
            customLoading,
        },
        data() {
            return {
				host:app.globalData.host,
				douyin:app.globalData.douyin,
                uploadHeader: {},
                form: {},
                rules: {
                    nickname: [{
                        required: true,
                        message: "昵称为必填项！"
                    }],
                    submitAge: [{
                        required: true,
                        message: "年龄为必填项！"
                    }],
                },
                loading: false,
            };
        },
        computed: {
            ...mapState({
                projectForm: (state) => state.projectForm,
            }),
        },
        methods: {
            ...mapMutations(["saveResult"]),
            jumpResult() {
                const _this = this;
                uni.request({
                    //这个位置替换你们服务器的地址
                    url: 'https://zilean.vip/zkl/dy/checkWord?word=' + encodeURIComponent(this.form.nickname),
                    method: 'GET',
                    success: (data) => {
                        const res = data.data;
                        console.log("调用接口 checkWord 返回", res); // 处理返回结果
                        if (res.data[0].predicts[0].hit) {
                            // 提示内容包含不良
                            uni.showToast({
                                title: '禁止不良字符',
                                icon: 'none'
                            });
                            return false;
                        } else {
                            // 提示内容OK
                            // uni.showToast({
                            //   title: '内容OK',
                            //   icon: 'success'
                            // });
                            _this.$refs.uForm.validate(async (valid) => {
                                if (valid) {
                                    _this.loading = true;
                                    uni.uploadFile({
                                        url: "https://zilean.vip/zkl/submit",
                                        filePath: _this.projectForm.tempFilePath,
                                        fileType: "audio",
                                        name: "file",
                                        header: _this.uploadHeader,
                                        formData: {
                                            ..._this.projectForm,
                                            ..._this.form,
                                            submitAge: Number(_this.form.submitAge),
                                        },
                                        success: (data) => {
                                            try {
                                                const res = JSON.parse(data.data);
                                                if (res.code == "0") {
                                                    _this.saveResult({
                                                        ...res.data
                                                    });
                                                    _this.loading = false;
                                                    // uni.navigateTo({
                                                    //   url: `/pages/project/result/index`,
                                                    // });
                                                    uni.navigateTo({
                                                        url: `/pages/project/loadingPage/index?from=personInfo`
                                                    });
                                                } else {
                                                    uni.showToast({
                                                        icon: "none",
                                                        title: res.msg
                                                            .includes('4') ?
                                                            '录音时长不能少于4秒！' :
                                                            '报告生成失败',
                                                        position: "top",
                                                    });
                                                    _this.loading = false;
                                                }
                                            } catch (error) {
                                                uni.showToast({
                                                    icon: "none",
                                                    title: "报告生成失败",
                                                    position: "top",
                                                });
                                                _this.loading = false;
                                                // uni.navigateTo({
                                                //   url: `/pages/project/result/index`,
                                                // });
                                                uni.navigateTo({
                                                    url: `/pages/project/loadingPage/index?from=personInfo`
                                                });
                                            }
                                        },
                                        fail: (err) => {
                                            uni.showToast({
                                                icon: "none",
                                                title: "报告生成失败",
                                                position: "top",
                                            });
                                            _this.loading = false;
                                        },
                                    });
                                } else {
                                    console.log("报告生成失败");
                                }
                            });
                        }
                    },
                    fail: (err) => {
                        console.error(err);
                    }
                });
            },
        },
        mounted() {
            this.$refs.uForm.setRules(this.rules);
        },
        onShareAppMessage() {
            return {
                title: '测一测你声音的年龄',
                path: '/pages/project/home/index',
                imageUrl: '../../../static/sharelogo.jpg'
            }
        },
    };
</script>

<style lang="scss" scoped>
    @import "./index.scss";
</style>
